<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
    <title>ShellCheck: SC2015 – Note that `A && B || C` is not if-then-else. C may run when A is true.</title>
    <link rel="stylesheet" href="css/bootstrap.min.css" />
  </head>
  <body style="margin-left: auto; margin-right: auto; max-width: 800px">
    <h1>SC2015 – ShellCheck Wiki</h1>
    <a href="https://github.com/koalaman/shellcheck/wiki/SC2015">See this page on GitHub</a>
    <p style="display: none"><a href="index.html">Sitemap</a></p>
    <hr />
    <h1
id="note-that-a--b--c-is-not-if-then-else-c-may-run-when-a-is-true">Note
that <code>A &amp;&amp; B || C</code> is not if-then-else. C may run
when A is true.</h1>
<h3 id="problematic-code">Problematic code:</h3>
<div class="sourceCode" id="cb1"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb1-1"><a href="SC2015.html#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="kw">[[</span> <span class="va">$dryrun</span> <span class="kw">]]</span> <span class="kw">&amp;&amp;</span> <span class="bu">echo</span> <span class="st">&quot;Would delete file&quot;</span> <span class="kw">||</span> <span class="fu">rm</span> file</span></code></pre></div>
<h3 id="correct-code">Correct code:</h3>
<div class="sourceCode" id="cb2"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb2-1"><a href="SC2015.html#cb2-1" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="kw">[[</span> <span class="va">$dryrun</span> <span class="kw">]]</span></span>
<span id="cb2-2"><a href="SC2015.html#cb2-2" aria-hidden="true" tabindex="-1"></a><span class="cf">then</span></span>
<span id="cb2-3"><a href="SC2015.html#cb2-3" aria-hidden="true" tabindex="-1"></a>  <span class="bu">echo</span> <span class="st">&quot;Would delete file&quot;</span></span>
<span id="cb2-4"><a href="SC2015.html#cb2-4" aria-hidden="true" tabindex="-1"></a><span class="cf">else</span></span>
<span id="cb2-5"><a href="SC2015.html#cb2-5" aria-hidden="true" tabindex="-1"></a>  <span class="fu">rm</span> file</span>
<span id="cb2-6"><a href="SC2015.html#cb2-6" aria-hidden="true" tabindex="-1"></a><span class="cf">fi</span></span></code></pre></div>
<h3 id="rationale">Rationale:</h3>
<p>It's common to use <code>A &amp;&amp; B</code> to run <code>B</code>
when <code>A</code> is true, and <code>A || C</code> to run
<code>C</code> when <code>A</code> is false.</p>
<p>However, combining them into <code>A &amp;&amp; B || C</code> is not
the same as <code>if A then B else C</code>.</p>
<p>In this case, if <code>A</code> is true but <code>B</code> is false,
<code>C</code> will run.</p>
<p>For the code sample above, if the script was run with stdout closed
for any reason (such as explicitly running
<code>script --dryrun &gt;&amp;-</code>), echo would fail and the file
would be deleted, even though <code>$dryrun</code> was set!</p>
<p>If an <code>if</code> clause is used instead, this problem is
avoided.</p>
<h3 id="boring-detail">Boring detail:</h3>
<p>We can think of the example above as</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode sh"><code class="sourceCode bash"><span id="cb3-1"><a href="SC2015.html#cb3-1" aria-hidden="true" tabindex="-1"></a><span class="kw">((([[</span> <span class="va">$dryrun</span> <span class="kw">]])</span> <span class="kw">&amp;&amp;</span> <span class="bu">echo</span> <span class="st">&quot;Would delete file&quot;</span><span class="kw">)</span> <span class="kw">||</span> <span class="fu">rm</span> file<span class="kw">)</span></span></code></pre></div>
<p>expressing the left-associativity of the <code>&amp;&amp;</code>
<code>||</code> operators.</p>
<p>Whenever a command (strictly, a pipeline) succeeds or fails, the
execution proceeds following the next <code>&amp;&amp;</code> (for
success) or <code>||</code> (for failure). (More strictly, the
parentheses should be replaced with <code>{ command; }</code> to avoid
making a subshell, but that's ugly and boring.)</p>
<h3 id="exceptions">Exceptions</h3>
<p>Ignore this warning when you actually do intend to run C when either
A or B fails.</p>
    <hr />
    <p style='font-size: 80%'><a href="../index.html">ShellCheck</a> is a static analysis tool for shell scripts. This page is part of its documentation.</p>
  </body>
</html>


